package com.handpay.nocardpay.baselibrary.qualificationinfo.model.impl;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.handpay.nocardpay.baselibrary.R;
import com.handpay.nocardpay.baselibrary.commbase.model.IBaseModel;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorInfo;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseErrorParser;
import com.handpay.nocardpay.baselibrary.commbase.model.bean.BaseResponseBean;
import com.handpay.nocardpay.baselibrary.config.NoCardPayApi;
import com.handpay.nocardpay.baselibrary.http.HttpClientImpl;
import com.handpay.nocardpay.baselibrary.http.HttpFileUpload;
import com.handpay.nocardpay.baselibrary.http.HttpHeadFactory;
import com.handpay.nocardpay.baselibrary.http.HttpListener;
import com.handpay.nocardpay.baselibrary.http.HttpRequest;
import com.handpay.nocardpay.baselibrary.logutils.HPLog;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.IQualification;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.IDCardStatus;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.QualificationQueryBean;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.QueryCertNoExporyBean;
import com.handpay.nocardpay.baselibrary.qualificationinfo.model.bean.ThreeElementResult;
import com.handpay.nocardpay.baselibrary.utils.UrlUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by fplei on 2017/8/11.
 * 资质信息相关业务(三要素检查，上传文件，上传资质，查询资质,获取补件信息，查询证件是否过期)
 */
public class QualificationModelImpl implements IQualification<ThreeElementResult,
        BaseResponseBean, QualificationQueryBean, IDCardStatus, QueryCertNoExporyBean> {
    private Context mContext;

    public QualificationModelImpl(Context context) {
        this.mContext = context;
    }

    @Override
    public void checkCardThreeElements(HashMap<String, Object> paramers, Class<ThreeElementResult> clzz, final IBaseModel.OnRequestFinishedListener<ThreeElementResult> listener) {
        String url = UrlUtils.calculateAction(NoCardPayApi.Do_CheckSwipeCardFourElements);
        final HttpRequest request = new HttpRequest.Builder(url)
                .setMethod(Request.Method.POST)
                .addheader(HttpHeadFactory.createRequestHeaders(mContext))
                .addParam(paramers).build();
        HttpClientImpl.gsonRequest(clzz, request, new HttpListener<ThreeElementResult>() {
            @Override
            public void onSuccess(ThreeElementResult result) {
                if (result == null) {
                    listener.onError(new BaseErrorInfo("-1", mContext.getString(R.string.paser_error)));
                    return;
                }
                if (result.isRequstSuccess()) {
                    listener.onSuccess(result);
                } else {
                    listener.onError(new BaseErrorInfo(result.responseCode, result.errMessage));
                }
            }

            @Override
            public void onError(VolleyError error) {
                listener.onError(new BaseErrorParser(error));
            }
        }, NoCardPayApi.Do_CheckSwipeCardFourElements);
    }

    @Override
    public BaseResponseBean uploadFile(HashMap<String, String> paramers, Class<BaseResponseBean> clzz, byte[] bytes, String action) {
        String url = UrlUtils.calculateAction(action);
        Map<String, String> headers = HttpHeadFactory.createRequestHeaders(mContext);
        String result = HttpFileUpload.uploadByteJson(mContext, url, headers, paramers, HttpClientImpl.DEFAULT_TIME_OUT, bytes);
        HPLog.i(HPLog.LFP_TAG, "QualificationModelImpl::uploadFile::result=" + result);
        Gson mGson = new Gson();
        BaseResponseBean responseBean = mGson.fromJson(result, clzz);
        return responseBean;
    }

    @Override
    public void submitQualificationData(HashMap<String, Object> paramers, Class<BaseResponseBean> clzz, final IBaseModel.OnRequestFinishedListener<BaseResponseBean> listener) {
        String url = UrlUtils.calculateAction(NoCardPayApi.Do_UploadCheckInfo);
        final HttpRequest request = new HttpRequest.Builder(url)
                .setMethod(Request.Method.POST)
                .addheader(HttpHeadFactory.createRequestHeaders(mContext))
                .addParam(paramers).build();
        HttpClientImpl.gsonRequest(clzz, request, new HttpListener<BaseResponseBean>() {
            @Override
            public void onSuccess(BaseResponseBean result) {
                if (result == null) {
                    listener.onError(new BaseErrorInfo("-1", mContext.getString(R.string.paser_error)));
                    return;
                }
                if (result.isRequstSuccess()) {
                    listener.onSuccess(result);
                } else {
                    listener.onError(new BaseErrorInfo(result.responseCode, result.errMessage));
                }
            }

            @Override
            public void onError(VolleyError error) {
                listener.onError(new BaseErrorParser(error));
            }
        }, NoCardPayApi.Do_UploadCheckInfo);
    }

    @Override
    public void queryQualificationCheckInfo(HashMap<String, Object> paramers, Class<QualificationQueryBean> clzz, final IBaseModel.OnRequestFinishedListener<QualificationQueryBean> listener) {
        String url = UrlUtils.calculateAction(NoCardPayApi.DO_zztGetAuditInfo);
        final HttpRequest request = new HttpRequest.Builder(url)
                .setMethod(Request.Method.POST)
                .addheader(HttpHeadFactory.createRequestHeaders(mContext))
                .addParam(paramers).build();
        HttpClientImpl.gsonRequest(clzz, request, new HttpListener<QualificationQueryBean>() {
            @Override
            public void onSuccess(QualificationQueryBean result) {
                if (result == null) {
                    listener.onError(new BaseErrorInfo("-1", mContext.getString(R.string.paser_error)));
                    return;
                }
                if (result.isRequstSuccess()) {
                    listener.onSuccess(result);
                } else {
                    listener.onError(new BaseErrorInfo(result.responseCode, result.errMessage));
                }
            }

            @Override
            public void onError(VolleyError error) {
                listener.onError(new BaseErrorParser(error));
            }
        }, NoCardPayApi.DO_zztGetAuditInfo);
    }

    @Override
    public void queryIDCardInfo(HashMap<String, Object> paramers, Class<IDCardStatus> clzz, final IBaseModel.OnRequestFinishedListener<IDCardStatus> listener) {
        String url = UrlUtils.calculateAction(NoCardPayApi.Do_tGetSupplyAuditInfo);
        final HttpRequest request = new HttpRequest.Builder(url)
                .setMethod(Request.Method.POST)
                .addheader(HttpHeadFactory.createRequestHeaders(mContext))
                .addParam(paramers).build();
        HttpClientImpl.gsonRequest(clzz, request, new HttpListener<IDCardStatus>() {
            @Override
            public void onSuccess(IDCardStatus result) {
                if (result == null) {
                    listener.onError(new BaseErrorInfo("-1", mContext.getString(R.string.paser_error)));
                    return;
                }
                if (result.isRequstSuccess()) {
                    listener.onSuccess(result);
                } else {
                    listener.onError(new BaseErrorInfo(result.responseCode, result.errMessage));
                }
            }

            @Override
            public void onError(VolleyError error) {
                listener.onError(new BaseErrorParser(error));
            }
        }, NoCardPayApi.Do_tGetSupplyAuditInfo);
    }

    @Override
    public void queryCertNoExpory(HashMap<String, Object> paramers, Class<QueryCertNoExporyBean> clzz, final IBaseModel.OnRequestFinishedListener<QueryCertNoExporyBean> listener) {
        String url = UrlUtils.calculateAction(NoCardPayApi.Do_QueryCertNoExpiryDate);
        final HttpRequest request = new HttpRequest.Builder(url)
                .setMethod(Request.Method.POST)
                .addheader(HttpHeadFactory.createRequestHeaders(mContext))
                .addParam(paramers).build();
        HttpClientImpl.gsonRequest(clzz, request, new HttpListener<QueryCertNoExporyBean>() {
            @Override
            public void onSuccess(QueryCertNoExporyBean result) {
                if (result == null) {
                    listener.onError(new BaseErrorInfo("-1", mContext.getString(R.string.paser_error)));
                    return;
                }
                if (result.isRequstSuccess()) {
                    listener.onSuccess(result);
                } else {
                    listener.onError(new BaseErrorInfo(result.responseCode, result.errMessage));
                }
            }

            @Override
            public void onError(VolleyError error) {
                listener.onError(new BaseErrorParser(error));
            }
        }, NoCardPayApi.Do_tGetSupplyAuditInfo);
    }
}
